package com.jastar.android.util;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

public class Alert {

    public static void toast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public static void info(Context context, String msg, DialogInterface.OnClickListener listener) {
        info(context, 0, msg, "确定", listener);
    }

    public static void info(Context context, int icon, String msg, DialogInterface.OnClickListener listener) {
        info(context, icon, msg, "确定", listener);
    }

    public static void info(Context context, int icon, String msg, String bText, DialogInterface.OnClickListener listener) {
        new AlertDialog.Builder(context).setIcon(icon).setTitle(msg).setPositiveButton(bText, listener).create().show();
    }

    public static void confirm(Context context, int icon, String msg, String bText, DialogInterface.OnClickListener listener, DialogInterface.OnClickListener cancelListener) {
        new AlertDialog.Builder(context).setIcon(icon).setTitle(msg).setPositiveButton(bText, listener)
                .setNegativeButton("取消", cancelListener).create().show();
    }

    public static void select(Context context, int icon, String msg, DialogInterface.OnClickListener listener, String... texts) {
        new AlertDialog.Builder(context).setIcon(icon).setTitle(msg).setItems(texts, (d, w) -> {
            d.dismiss();
            new Handler().post(() -> listener.onClick(d, w));
        }).create().show();
    }

}
